package com.wyr.dynamic_proxy.jdk;

import com.wyr.dynamic_proxy.service.UserService;
import com.wyr.dynamic_proxy.service.impl.UserServiceImpl;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyInvocationHandler implements InvocationHandler {
    public UserService userService;

    //设置被代理对象
    public void setTrueObject(Object object){
        this.userService=(UserService) object;
    }

    public Object getProxy(){
        return Proxy.newProxyInstance(this.getClass().getClassLoader(),userService.getClass().getInterfaces(),this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("---------我是jdk动态代理------------");
        Object result = method.invoke(userService, args);
        return result;
    }
}
